package com.byx.chatserver.controller;

import com.byx.chatserver.dto.FriendGroupDto;
import com.byx.chatserver.dto.GroupDto;
import com.byx.chatserver.dto.SimpleGroupDto;
import com.byx.chatserver.dto.SimpleUserDto;
import com.byx.chatserver.entity.FileType;
import com.byx.chatserver.entity.Group;
import com.byx.chatserver.entity.User;
import com.byx.chatserver.service.IFileService;
import com.byx.chatserver.service.IUserActionService;
import com.byx.springboot.unifiedresponseresult.entity.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ActionController
 *
 * @author Superb
 * @date 2021/4/29 10:23
 */
@RestController
@ResponseResult
@RequestMapping("/action")
public class ActionController {
    private IUserActionService userActionService;
    private IFileService fileService;

    @Autowired
    public void setFileService(@Qualifier("fileServiceImpl") IFileService fileService) {
        this.fileService = fileService;
    }

    @Autowired
    public void setUserActionService(@Qualifier("userActionServiceImpl") IUserActionService userActionService) {
        this.userActionService = userActionService;
    }


    @GetMapping("/getUserById")
    public User getUserById(Long id){
        return userActionService.getUserInfoById(id);
    }

    @GetMapping("/getUserByLoginId")
    public User getUserByLoginId(String loginId){
        return userActionService.getUserInfoByLoginId(loginId);
    }

    @PostMapping("/fuzzySearchAll")
    public Map<String,Object> fuzzySearchAll(@RequestParam("searchKey") String searchKey){
        List<SimpleUserDto> users = userActionService.fuzzySearchUser(searchKey,true);
        List<SimpleGroupDto> groups = userActionService.fuzzySearchGroup(searchKey,true);
        Map<String, Object> map = new HashMap<>(2);
        map.put("users",users);
        map.put("groups",groups);
        return map;
    }

    @PostMapping("/fuzzySearchUser")
    public List<SimpleUserDto> fuzzySearchUser(String searchKey){
        return userActionService.fuzzySearchUser(searchKey,false);
    }

    @PostMapping("/fuzzySearchGroup")
    public List<SimpleGroupDto> fuzzySearchGroup(String searchKey){
        return userActionService.fuzzySearchGroup(searchKey,false);
    }

    @GetMapping("/queryFriendGroup/{id}")
    public List<FriendGroupDto> queryFriendGroup(@PathVariable("id") Long id){
        return userActionService.getUserFriendGroup(id);
    }

    @PostMapping("/addFriendGroup")
    public Integer addFriendGroup(@RequestBody FriendGroupDto friendGroupDto){
        return userActionService.addFriendGroup(friendGroupDto);
    }

    @GetMapping("/queryUserSimpleInfo/{id}")
    public User queryUserSimpleInfo(@PathVariable("id") Long id){
        return userActionService.queryUserSimpleInfoById(id);
    }

    @GetMapping("/queryUserGroup/{id}")
    public Map<Integer, List<SimpleGroupDto>> queryUserGroup(@PathVariable("id") Long id){
        return userActionService.queryUserGroupById(id);
    }

    @GetMapping("/queryUserGroupDetail")
    public GroupDto queryUserGroupDetail(@RequestParam("userId") Long userId, @RequestParam("groupId") Long groupId){
        return userActionService.queryUserGroupDetail(userId,groupId);
    }

    @PostMapping("/changeUserInfo")
    public void changeUserInfo(@RequestBody User user){
        userActionService.changeUserInfo(user);
    }

    @PostMapping(value = "/changeUserAvatar",consumes = "multipart/*",headers = "Content-Type=multipart/form-data")
    public String changeUserAvatar(@RequestParam("userId") String userId, @RequestParam("file") MultipartFile file,@RequestParam("timestamp") Long timestamp){
        String fileUrl = fileService.saveFile(file, userId, FileType.AVATAR);
        userActionService.changeUserAvatar(userId, fileUrl,timestamp);
        return fileUrl;
    }

    @PostMapping("/createGroup")
    public void createGroup(@RequestBody Group group){
        userActionService.createGroup(group);
    }

    @PostMapping("/insertGroupUser")
    public void insertGroupUser(Long userId,Long groupId,String nickName){
        userActionService.requestJoinGroupWithNoResp(userId,groupId,nickName);
    }
}
